package com.kun.base.util

import android.text.TextUtils
import android.util.Log

/**
 * @author mrhu
 */
object LogUtils {
    /** Log输出的控制开关  */
    var isDebug = true
    const val SELF_FlAG = "WeiLian"
    fun i(mes: String?) {
        i(SELF_FlAG, mes)
    }

    fun i(objTag: Any, msg: String?) {
        if (!isDebug) {
            return
        }
        var tag: String

        // 如果objTag是String，则直接使用
        // 如果objTag不是String，则使用它的类名
        // 如果在匿名内部类，写this的话是识别不了该类，所以获取当前对象全类名来分隔
        if (objTag is String) {
            tag = objTag
        } else if (objTag is Class<*>) {
            tag = objTag.simpleName
        } else {
            tag = objTag.javaClass.name
            val split = tag.split("\\.".toRegex()).toTypedArray()
            tag = split[split.size - 1].split("\\$".toRegex()).toTypedArray()[0]
        }
        if (TextUtils.isEmpty(msg)) {
            Log.i(SELF_FlAG + tag, "该log输出信息为空")
        } else {
            Log.i(SELF_FlAG + tag, msg!!)
        }
    }

    fun e(mes: String?) {
        e(SELF_FlAG, mes)
    }

    /**
     * 错误调试信息
     * @param objTag
     * @param msg
     */
    fun e(objTag: Any, msg: String?) {
        if (!isDebug) {
            return
        }
        var tag: String
        if (objTag is String) {
            tag = objTag
        } else if (objTag is Class<*>) {
            tag = objTag.simpleName
        } else {
            tag = objTag.javaClass.name
            val split = tag.split("\\.".toRegex()).toTypedArray()
            tag = split[split.size - 1].split("\\$".toRegex()).toTypedArray()[0]
        }
        if (TextUtils.isEmpty(msg)) {
            Log.e(SELF_FlAG + tag, "该log输出信息为空")
        } else {
            Log.e(SELF_FlAG + tag, msg!!)
        }
    }

    fun v(mes: String?) {
        v(SELF_FlAG, mes)
    }

    /**
     * 详细输出调试
     * @param objTag
     * @param msg
     */
    fun v(objTag: Any, msg: String?) {
        if (!isDebug) {
            return
        }
        var tag: String
        if (objTag is String) {
            tag = objTag
        } else if (objTag is Class<*>) {
            tag = objTag.simpleName
        } else {
            tag = objTag.javaClass.name
            val split = tag.split("\\.".toRegex()).toTypedArray()
            tag = split[split.size - 1].split("\\$".toRegex()).toTypedArray()[0]
        }
        if (TextUtils.isEmpty(msg)) {
            Log.v(SELF_FlAG + tag, "该log输出信息为空")
        } else {
            Log.v(SELF_FlAG + tag, msg!!)
        }
    }

    fun w(mes: String?) {
        w(SELF_FlAG, mes)
    }

    /**
     * 警告的调试信息
     * @param objTag
     * @param msg
     */
    fun w(objTag: Any, msg: String?) {
        if (!isDebug) {
            return
        }
        var tag: String
        if (objTag is String) {
            tag = objTag
        } else if (objTag is Class<*>) {
            tag = objTag.simpleName
        } else {
            tag = objTag.javaClass.name
            val split = tag.split("\\.".toRegex()).toTypedArray()
            tag = split[split.size - 1].split("\\$".toRegex()).toTypedArray()[0]
        }
        if (TextUtils.isEmpty(msg)) {
            Log.w(SELF_FlAG + tag, "该log输出信息为空")
        } else {
            Log.w(SELF_FlAG + tag, msg!!)
        }
    }

    fun d(mes: String?) {
        d(SELF_FlAG, mes)
    }

    /**
     * debug输出调试
     * @param objTag
     * @param msg
     */
    fun d(objTag: Any, msg: String?) {
        if (!isDebug) {
            return
        }
        var tag: String
        if (objTag is String) {
            tag = objTag
        } else if (objTag is Class<*>) {
            tag = objTag.simpleName
        } else {
            tag = objTag.javaClass.name
            val split = tag.split("\\.".toRegex()).toTypedArray()
            tag = split[split.size - 1].split("\\$".toRegex()).toTypedArray()[0]
        }
        if (TextUtils.isEmpty(msg)) {
            Log.d(SELF_FlAG + tag, "该log输出信息为空")
        } else {
            Log.d(SELF_FlAG + tag, msg!!)
        }
    }
}